/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test;

import java.awt.AWTException;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.io.File;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import model.Plant;
import org.junit.Test;
import static org.junit.Assert.*;


public class SaveLoadGUI extends GUITestBase {
    
    protected File loadFile = new File("save.ser");
    
    @Override
    public void setUp() throws AWTException {
        super.setUp();
        if (loadFile != null) {
            loadFile.delete();
        }
    }
    
    @Override
    public void tearDown() {
        super.tearDown();
    }
    
    @Test
    public void testLoadUnexisting() throws AWTException {
        
        int oldPlantHash = presenter.getPlant().hashCode();
        
        // click on load action from the menu
        pressMouse(gui.jMenuBar.getMenu(0).getLocationOnScreen());
        pressMouse(gui.jMenuBar.getMenu(0).getItem(2).getLocationOnScreen());
        keyPress(KeyEvent.VK_ENTER); // yes to screen warning us that unsaved changes will be lost
        keyPress(KeyEvent.VK_ENTER); // yes to confirmation / error screen
        
        assertEquals("Plant should have not changed", oldPlantHash, presenter.getPlant().hashCode());
    }
    
    @Test
    public void testLoadExisting() throws AWTException {
        presenter.newGame("testName1");
        presenter.saveGame();
        
        presenter.newGame("testName2");
        
        assertEquals("testName2", presenter.getPlant().getOperatorName());
        
        // load game from GUI
        pressMouse(gui.jMenuBar.getMenu(0));
        pressMouse(gui.jMenuBar.getMenu(0).getItem(2));
        keyPress(KeyEvent.VK_ENTER);
        keyPress(KeyEvent.VK_ENTER);
        
        assertEquals("Old plant loaded", "testName1", presenter.getPlant().getOperatorName());       
    }
    
    @Test
    public void testLoadFromGameOver() throws AWTException {
        presenter.newGame("testName");
        presenter.saveGame();
        
        presenter.newGame("notthisone");
        presenter.setControlRods(0);
        presenter.step(100);
        rob.delay(2000);
        pressMouse(gui.jButtonStep);
        
        // seletect load button
        // this is a horrible and unportable hack, but we're using
        // robots, so we're grateful they are even able to distinguish
        // between the left and right mouse buttons (ie. are not very smart)
        keyPress(KeyEvent.VK_TAB);
        keyPress(KeyEvent.VK_TAB);
        keyPress(KeyEvent.VK_ENTER);
        keyPress(KeyEvent.VK_ENTER);
        
        assertEquals("Previous game not loaded", "testName", presenter.getPlant().getOperatorName());
        
    }
}
